linux centos 8下安装nginx

linux版本:CentOS8 64位

安装命令:

安装依赖
yum -y install gcc pcre-devel zlib-devel openssl openssl-devel

 nginx下载地址:https://nginx.org/download/
下载“nginx-1.9.9.tar.gz”,移动到/home/下。 
1.解压
tar -zxvf nginx-1.9.9.tar.gz

2.进入nginx目录
cd nginx-1.9.9
3.配置
./configure --prefix=/usr/local/nginx
4.安装make
make
make install
安装如图在浏览器输入地址测试是否成功

编译错误

分析原因:

是将警告当成了错误处理,打开 nginx的安装目录/objs/Makefile,去掉CFLAGS中的-Werror,再重新make

-Wall 表示打开gcc的所有警告
-Werror,它要求gcc将所有的警告当成错误进行处理

请在make后再修改这个文件,修改后再make install

如果出现下面错误

这里提示我们struct crypt_data’没有名为‘current_salt’的成员:cd.current_salt[0] = ~salt[0];

最好的办法是换一个版本,因为条件限制,我们就进到源码里把这行直接注释掉好了

vim src/os/unix/ngx_user.c

NGINX配置虚拟主机支持PHP

在nginx.conf的http段添加include /www/server/nginx/conf/vhost/*.conf;

在/www/server/nginx/conf/vhost/中添加文件www.1.com.conf内容如下

server{

	
	
	listen 80;
    #listen [::]:80;
    server_name 1.com www.1.com;
    index index.php index.html index.htm default.php default.htm default.html;
    root /www/wwwroot/www.1.com;
	
	
	#自己配置
	#include vhostpath.conf;
	
	
		location ~* \.php$ {
			#root /www/wwwroot/www.1.com;
			fastcgi_pass   127.0.0.1:9000;
			fastcgi_index  index.php;
			fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
			include        fastcgi_params;
		}	
	
   
	
}

设置开机启动

设置软连接

ln -sf /www/server/nginx/sbin/nginx /usr/bin/nginx
cp /www/init.d/nginx /etc/init.d/nginx
cp/www/init.d/nginx.service /etc/systemd/system/nginx.service
chmod +x /etc/init.d/nginx

复制nginx文件内容如下面所示

#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
# run 'update-rc.d -f nginx defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add nginx'

### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFO

# Author:   zhouchong
# website:  http://www.zc10.cn

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=nginx
NGINX_BIN=/www/server/nginx/sbin/$NAME
CONFIGFILE=/www/server/nginx/conf/$NAME.conf
PIDFILE=/www/server/nginx/logs/$NAME.pid
ulimit -n 8192
case "$1" in
    start)
        echo -n "Starting $NAME... "
		if [ -f $PIDFILE ];then
			mPID=`cat $PIDFILE`
			isStart=`ps ax | awk '{ print $1 }' | grep -e "^${mPID}$"`
			if [ "$isStart" != '' ];then
				echo "$NAME (pid `pidof $NAME`) already running."
				exit 1
			fi
		fi

        $NGINX_BIN -c $CONFIGFILE

        if [ "$?" != 0 ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
        ;;

    stop)
        echo -n "Stoping $NAME... "
		if [ -f $PIDFILE ];then
			mPID=`cat $PIDFILE`
			isStart=`ps ax | awk '{ print $1 }' | grep -e "^${mPID}$"`
			if [ "$isStart" = '' ];then
				echo "$NAME is not running."
				exit 1
			fi
		else
			echo "$NAME is not running."
			exit 1
        fi
        $NGINX_BIN -s stop

        if [ "$?" != 0 ] ; then
            echo " failed. Use force-quit"
            exit 1
        else
            echo " done"
        fi
        ;;

    status)
		if [ -f $PIDFILE ];then
			mPID=`cat $PIDFILE`
			isStart=`ps ax | awk '{ print $1 }' | grep -e "^${mPID}$"`
			if [ "$isStart" != '' ];then
				echo "$NAME (pid `pidof $NAME`) already running."
				exit 1
			else
				echo "$NAME is stopped"
				exit 0
			fi
		else
			echo "$NAME is stopped"
			exit 0
        fi
        ;;
    restart)
        $0 stop
        sleep 1
        $0 start
        ;;

    reload)
        echo -n "Reload service $NAME... "
		if [ -f $PIDFILE ];then
			mPID=`cat $PIDFILE`
			isStart=`ps ax | awk '{ print $1 }' | grep -e "^${mPID}$"`
			if [ "$isStart" != '' ];then
				$NGINX_BIN -s reload
				echo " done"
			else
				echo "$NAME is not running, can't reload."
				exit 1
			fi
		else
			echo "$NAME is not running, can't reload."
			exit 1
		fi
        ;;

    configtest)
        echo -n "Test $NAME configure files... "
        $NGINX_BIN -t
        ;;

    *)
        echo "Usage: $0 {start|stop|restart|reload|status|configtest}"
        exit 1
        ;;
esac

nginx.service 内容

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/www/server/nginx/logs/nginx.pid
ExecStart=/www/server/nginx/sbin/nginx -c /www/server/nginx/conf/nginx.conf
ExecReload=/www/server/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=false

[Install]
WantedBy=multi-user.target

查看当前状态
systemctl start nginx .service

设置开启其他

systemctl enable nginx .service

刷新配置文件

systemctl daemon-reload

systemctl list-unit-files|grep nginx

出现nginx.service enabled 表示成功开启

赞(1)
未经允许不得转载:程序员中文网 » linux centos 8下安装nginx
分享到: 更多 (0)
关于我们 免责申明 意见反馈 隐私政策
程序员中文网:公益在线网站,帮助学习者快速成长!
关注微信 技术交流
推荐文章
每天精选资源文章推送
推荐文章
随时随地碎片化学习
推荐文章
发现有趣的